#include "serialportpro.h"
#include <QDebug>

Serialportpro::Serialportpro ( QObject* parent)
{
    m_serial = new QSerialPort(parent);    
    m_serial->setPortName("/dev/ttymxc1");
    bool ret = m_serial->open(QIODevice::ReadWrite);
    m_serial->setBaudRate(57600);
    m_serial->setDataBits(QSerialPort::Data8);
    m_serial->setParity(QSerialPort::NoParity);
    m_serial->setStopBits(QSerialPort::OneStop);
    m_serial->setFlowControl(QSerialPort::NoFlowControl);


  // if(ret)
    {
        m_timer = new QTimer;
        QObject::connect( m_timer, SIGNAL( timeout() ), this, SLOT( comunicate() ) );
        m_timer->start( 10 );
    }

    test_dir = 1;
    test_flr = 1;
    QTimer * t_timer =  new QTimer;
    QObject::connect( t_timer, SIGNAL( timeout() ), this, SLOT( test() ) );
    t_timer->start( 500 );

    m_isPrograming = false;
    txrepeat = false;
    lift_N = 0;
}

void Serialportpro:: comunicate()
{
//    QByteArray rxtmp = m_serial->readAll();
//    if(rxtmp.size()>0)
//    {
//        //qDebug()<<"hehe: "<<rxtmp.toHex();
//        m_rxdata += rxtmp;
//    }

    demux();    

}

void Serialportpro:: demux()
{
    char asc[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D','E', 'F'};
    int i=1;

    while ( m_rxdata.size() > 3 )
    {
        
        if(m_rxdata[0]!='\x02')
        {
              m_rxdata.remove( 0, 1 );
        }
        else
        {

                if(m_rxdata.size() < 140)
                    return;
                if(m_rxdata[139] == '\x03')
                {

                    int Sum = 0;
                    for(i = 1;i<136;i++)
                        Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {

                        QByteArray curdir = m_rxdata.mid(1,1);
                        bool ok;
                        int dir = curdir.toInt(&ok,16);                       
                        emit dir_s(dir);

                        QByteArray curflr = m_rxdata.mid(2,3);
                        //int flr = curflr.toInt(&ok,10);
                        emit flr_s(curflr);

                        //QByteArray curstate = m_rxdata.mid(5,1);
                        //emit state_s(curstate);

                        QByteArray curbl= m_rxdata.mid(7,1);
                        if(curbl[0]=='0')
                        {
                            //开背光
                        }
                        else
                        {
                            //关背光
                        }

                        for(i=0;i<16;i++)
                        {
                            QByteArray regfstate = m_rxdata.mid(8+16*lift_N+i,1);
                            int regf = regfstate.toInt(&ok,16);
                            for(int j=0;j<4;j++)
                            {

                                if(regf&(1<<j))
                                {
                                    emit regflr_s(i*4+j);
                                }
                                else
                                {
                                    emit unregflr_s(i*4+j);
                                }
                            }
                        }

                        m_rxdata.remove( 0, 140 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
        }
    }
}

void  Serialportpro::test()
{
    char asc[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D','E', 'F'};
    int i=1;
    QByteArray newprotocol1;
    newprotocol1.resize ( 140 );
    newprotocol1[0] = 0x02;
    if(test_dir==1)
        newprotocol1[1] = 0x31;
    else
        newprotocol1[1] = 0x33;



    if(test_flr>20)
         newprotocol1[2] = 'M';
    else
        newprotocol1[2] = ' ';

    if(test_flr<10)
        newprotocol1[3] = 0x20;
    else
        newprotocol1[3] = 0x30+test_flr/10;

    newprotocol1[4] = 0x30+test_flr%10;//N楼
    newprotocol1[5] = 0x30;
    newprotocol1[6] = 0x30;
    newprotocol1[7] = 0x30;

    for(int j=0;j<8;j++)
    for(i=0;i<16;i++)
    {
       newprotocol1[8+16*j+i] = asc[test_flr%16];
    }

    int Sum = 0;
    for(i = 1;i<136;i++)
    Sum ^= newprotocol1[i];
    newprotocol1[136]=asc[Sum&0x0f];//校验F
    newprotocol1[137] = 0x30;
    newprotocol1[138] = 0x30;
    newprotocol1[139] = 0x03;

    //qDebug() << newprotocol1.toHex();

    m_rxdata += newprotocol1.mid(0,140);

    test_flr += test_dir;
     if(test_flr>28)
     {
            test_dir = -1;
            test_flr = 28;
     }
     else if(test_flr<1)
     {
         test_dir = 1;
         test_flr = 1;
     }

}
